﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LitJson;
using System.IO;

public class ReadCard : MonoBehaviour
{
    private static ReadCard _instance;

    /// <summary>
    /// 所有的卡片
    /// </summary>
    public List<Cards> goodCard;

    public List<Cards> nunormalCard;
    public List<Cards> badCard;

    //public enum state
    //{
    //    N1 = 0,
    //    N2 = 1,
    //    N3 = 2,
    //}

    //public state stateNow;
    public int states;

    public JsonData CardDate;

    public static ReadCard Instance
    {
        set
        {
            if (_instance == null)
            {
                _instance = GameObject.Find("Boot").GetComponent<ReadCard>();
            }
        }

        get
        {
            if (_instance == null)
            {
                _instance = GameObject.Find("Boot").GetComponent<ReadCard>();
            }

            return _instance;
        }
    }


    void Awake()
    {
        states = 0;
        ReadDate();
    }

    //读取json里的数据，建立大卡池
    public void ReadDate()
    {
        CardDate = new JsonData();
        TextAsset json = Resources.Load<TextAsset>("Prefabs/cardDate");

        CardDate = JsonMapper.ToObject(json.text);

        goodCard = new List<Cards>();
        nunormalCard = new List<Cards>();
        badCard = new List<Cards>();

        for (int i = 0; i < CardDate.Count; i++)
        {
            Cards temp = new Cards();
            temp.mydate.Name = (string) CardDate[i]["Name"];
            temp.mydate.Descript = (string) CardDate[i]["Descript"];
            temp.mydate.Odds = (float) CardDate[i]["Odds"];
            temp.mydate.MinHealth = (float) CardDate[i]["MinHealth"];
            temp.mydate.MaxHealth = (float) CardDate[i]["MaxHealth"];
            temp.mydate.MinMood = (float) CardDate[i]["MinMood"];
            temp.mydate.MaxMood = (float) CardDate[i]["MaxMood"];
            temp.mydate.Radion1 = (string) CardDate[i]["Radion1"];
            temp.mydate.Radion2 = (string) CardDate[i]["Radion2"];
            //temp.mydate.Radion3 = (string) CardDate[i]["Radion3"];
            int intVal = (int) (CardDate[i]["EnumeCardsType"]);
            temp.mydate.Type = (Cards.Enume_CardsType) intVal;
            int intVal2 = (int) (CardDate[i]["CardPool"]);
            temp.mydate.cardPool = (Cards.CardPool) intVal2;

            switch (temp.mydate.cardPool)
            {
                case Cards.CardPool.good:
                    goodCard.Add(temp);
                    break;
                case Cards.CardPool.nunormal:
                    nunormalCard.Add(temp);
                    break;
                case Cards.CardPool.bad:
                    badCard.Add(temp);
                    break;
                default:
                    Debug.Log("有卡没有加入卡池，卡的名字叫：" + temp.mydate.Name);
                    break;
            }
        }

        ///Debug.Log("good"+goodCard.Count);
        //Debug.Log("nunormal" + nunormalCard.Count );
        //Debug.Log("bad" + badCard.Count );

        //for (int i = 0; i < goodCard.Count; i++)
        //{
        //    Debug.Log(goodCard[i].mydate.Name);
        //}

        //for (int i = 0; i < nunormalCard.Count; i++)
        //{
        //    Debug.Log(nunormalCard[i].mydate.Name);
        //}

        //for (int i = 0; i < badCard.Count; i++)
        //{
        //    Debug.Log(badCard[i].mydate.Name);
        //}
    }

    public Cards.AttributeCard RandomMy(float m_health, float m_mood)
    {
        float temp = m_health + m_mood;
        //苛刻条件
        if (states == 0)
        {
            states = 1;
            //Debug.Log(goodCard.Count);
            
            return goodCard[Random.Range(0, goodCard.Count)].mydate;
            
        }
        //比较苛刻条件
        else if (states == 1)
        {
            states = 2;
            //Debug.Log("普通池子");
            return nunormalCard[Random.Range(0, nunormalCard.Count)].mydate;
        }
        else if (states==2)
        {
            states = 0;
            //Debug.Log("好池子");
            return badCard[Random.Range(0, badCard.Count)].mydate;
        }
        else
        {
            //Debug.Log("好池子");
            return badCard[Random.Range(0, badCard.Count)].mydate;
        }

    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log(RandomMy(Random.Range(0, 1000), Random.Range(0, 1000)).Name);
        }
    }
}